<?php
// Init Form
$form = $this->form;
$form->setAttribute('action', $this->url('user/profile'));
$form->prepare();

$user = $form->get('user');

// Configure Errors Helper
$errorsHelper = $this->plugin('FormElementErrors');
$errorsHelper->setMessageOpenFormat('<div class="help-block">')
			 ->setMessageCloseString('</div>');
?>

<div class="row-fluid">
	<?php echo $this->form()->openTag($form); ?>
	<fieldset>
		<legend><?php echo $this->translate('Profile') ?></legend>
		<?php
	    $elem = $user->get('name');
	    $errors = $elem->getMessages($elem);
	    $errorClass = (!empty($errors)) ? ' has-error' : '';
	    ?>
		<div class="form-group<?php echo $errorClass ?>">
			<?php echo $this->formLabel($elem); ?>
			<div class="col-lg-3">
				<?php echo $this->formInput($elem); ?>
				<?php echo $errorsHelper($elem); ?>
			</div>
		</div>
		
		<?php
	    $elem = $user->get('gender');
	    $errors = $elem->getMessages($elem);
	    $errorClass = (!empty($errors)) ? ' has-error' : '';
	    ?>
		<div class="form-group<?php echo $errorClass ?>">
			<?php echo $this->formLabel($elem); ?>
			<div class="col-lg-3">
				<?php echo $this->formSelect($elem); ?>
				<?php echo $errorsHelper($elem); ?>
			</div>
		</div>
		
		<?php
	    $elem = $user->get('dateBirth');
	    $errors = $elem->getMessages($elem);
	    $errorClass = (!empty($errors)) ? ' has-error' : '';
	    ?>
		<div class="form-group<?php echo $errorClass ?>">
			<?php echo $this->formLabel($elem); ?>
			<div class="col-lg-3">
				<?php echo $this->formInput($elem); ?>
				<?php echo $errorsHelper($elem); ?>
			</div>
		</div>
		
		<?php
	    $elem = $user->get('cpf');
	    $errors = $elem->getMessages($elem);
	    $errorClass = (!empty($errors)) ? ' has-error' : '';
	    ?>
		<div class="form-group<?php echo $errorClass ?>">
			<?php echo $this->formLabel($elem); ?>
			<div class="col-lg-3">
				<?php echo $this->formInput($elem); ?>
				<?php echo $errorsHelper($elem); ?>
			</div>
		</div>
		
		<?php
	    $elem = $user->get('job');
	    $errors = $elem->getMessages($elem);
	    $errorClass = (!empty($errors)) ? ' has-error' : '';
	    ?>
		<div class="form-group<?php echo $errorClass ?>">
			<?php echo $this->formLabel($elem); ?>
			<div class="col-lg-3">
				<?php echo $this->formInput($elem); ?>
				<?php echo $errorsHelper($elem); ?>
			</div>
		</div>
		
		<?php
	    $elem = $user->get('phone1');
	    $errors = $elem->getMessages($elem);
	    $errorClass = (!empty($errors)) ? ' has-error' : '';
	    ?>
		<div class="form-group<?php echo $errorClass ?>">
			<?php echo $this->formLabel($elem); ?>
			<div class="col-lg-3">
				<?php echo $this->formInput($elem); ?>
				<?php echo $errorsHelper($elem); ?>
			</div>
		</div>
		
		<?php
	    $elem = $user->get('phone2');
	    $errors = $elem->getMessages($elem);
	    $errorClass = (!empty($errors)) ? ' has-error' : '';
	    ?>
		<div class="form-group<?php echo $errorClass ?>">
			<?php echo $this->formLabel($elem); ?>
			<div class="col-lg-3">
				<?php echo $this->formInput($elem); ?>
				<?php echo $errorsHelper($elem); ?>
			</div>
		</div>
		
		<?php $addresses  = $user->get('addresses'); ?>
		<?php foreach ($addresses as $address): ?>
			<?php
			$elem = $address->get('countryId');
		    $errors = $elem->getMessages($elem);
		    $errorClass = (!empty($errors)) ? ' has-error' : '';
		    ?>
			<div class="form-group<?php echo $errorClass ?>">
				<?php echo $this->formLabel($elem); ?>
				<div class="col-lg-3">
					<?php echo $this->formSelect($elem); ?>
					<?php echo $errorsHelper($elem); ?>
				</div>
			</div>
			
			<?php
			$elem = $address->get('address');
		    $errors = $elem->getMessages($elem);
		    $errorClass = (!empty($errors)) ? ' has-error' : '';
		    ?>
			<div class="form-group<?php echo $errorClass ?>">
				<?php echo $this->formLabel($elem); ?>
				<div class="col-lg-3">
					<?php echo $this->formInput($elem); ?>
					<?php echo $errorsHelper($elem); ?>
				</div>
			</div>
			
			<?php
			$elem = $address->get('number');
		    $errors = $elem->getMessages($elem);
		    $errorClass = (!empty($errors)) ? ' has-error' : '';
		    ?>
			<div class="form-group<?php echo $errorClass ?>">
				<?php echo $this->formLabel($elem); ?>
				<div class="col-lg-3">
					<?php echo $this->formInput($elem); ?>
					<?php echo $errorsHelper($elem); ?>
				</div>
			</div>
			
			<?php
			$elem = $address->get('postcode');
		    $errors = $elem->getMessages($elem);
		    $errorClass = (!empty($errors)) ? ' has-error' : '';
		    ?>
			<div class="form-group<?php echo $errorClass ?>">
				<?php echo $this->formLabel($elem); ?>
				<div class="col-lg-3">
					<?php echo $this->formInput($elem); ?>
					<?php echo $errorsHelper($elem); ?>
				</div>
			</div>
		<?php endforeach ?>
		
		<?php
	    $elem = $user->get('biography');
	    $errors = $elem->getMessages($elem);
	    $errorClass = (!empty($errors)) ? ' has-error' : '';
	    ?>
		<div class="form-group<?php echo $errorClass ?>">
			<?php echo $this->formLabel($elem); ?>
			<div class="col-lg-3">
				<?php echo $this->formTextArea($elem); ?>
				<?php echo $errorsHelper($elem); ?>
			</div>
		</div>
		
		<?php foreach ($form as $elem): ?>
			<?php if($elem instanceof Zend\Form\Element\Captcha): ?>
			<?php
		    $errors = $elem->getMessages($elem);
		    $errorClass = (!empty($errors)) ? ' has-error' : '';
		    ?>
			<div class="form-group<?php echo $errorClass ?>">
				<?php echo $this->formLabel($elem); ?>
				<div class="col-lg-3">
					<?php echo $this->formCaptcha($elem); ?>
					<?php echo $errorsHelper($elem); ?>
				</div>
			</div>
			<?php elseif ($elem instanceof Zend\Form\Element\Submit): ?>
			<?php echo $this->formSubmit($elem) ?>
			<?php endif ?>
		<?php endforeach ?>
	</fieldset>
	<?php echo $this->form()->closeTag(); ?>
</div>